﻿using System;
using System.Collections.Generic;

namespace Gonte.Types
{
    /// <summary>
    /// The template to create the dynamic object from
    /// </summary>
    public class DynamicClass
    {
        #region Constructors

        private DynamicClass(string name)
        {
            name_ = name;
        } 

        #endregion

        #region Methods

        public static DynamicClass GetClass(string name)
        {
            lock (lock_)
            {
                if (!classes_.ContainsKey(name))
                {
                    classes_.Add(name, new DynamicClass(name));
                }
            }

            return classes_[name];
        }

        public DynamicObject CreateInstance()
        {
            return new DynamicObject(this);
        }

        #endregion

        #region Properties

        /// <summary>
        /// The name of the class
        /// </summary>
        public string Name
        {
            get { return name_; }
            //set { name_ = value; }
        }

        /// <summary>
        /// The field information
        /// </summary>
        public Dictionary<string, DynamicFieldInfo> FieldsInfo
        {
            get { return fieldsInfo_; }
            //set { fieldsInfo_ = value; }
        } 

        #endregion

        #region Fields

        // Dynamic class members
        private readonly string name_;
        private Dictionary<string, DynamicFieldInfo> fieldsInfo_ = new Dictionary<string, DynamicFieldInfo>(); 

        // Cache the dynamic classes (types) for reuse
        private static Dictionary<string, DynamicClass> classes_ = new Dictionary<string, DynamicClass>();
        static readonly Object lock_ = new Object(); 

        #endregion
    }
}
